package com.hfut.eeg.controller;

import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.common.component.EEGFileMonitor;
import com.hfut.eeg.common.util.DatabaseGenerator;
import com.hfut.eeg.service.EEGService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.controller
 * @Description: TODO
 * @date 2019-11-06 19:47
 */
@Controller
@Api(tags = "EggTestController", value = "提供egg测试数据")
@RequestMapping("/user")
public class EggTestController {

    private static final int CHANNEL = 16;
    private static final int LEVEL = 60;

    @Autowired
    private EEGFileMonitor monitor;

    @Autowired
    private DatabaseGenerator generator;

    @Autowired
    private EEGService service;
    @ApiOperation("用户登录")
    @RequestMapping(value = "/getEggData")
    @ResponseBody
    public CommonResult getEggData(){
        //generator.init();
        //monitor.init();

        //return CommonResult.success("----");
        return CommonResult.success(getFileContent("src/main/resources/static/file/A1A2.txt"));

    }

    private static ArrayList<ArrayList<Double>> getFileContent (String filePath){

        ArrayList<String> labels = new ArrayList<>();
        ArrayList<ArrayList<Double>> datas = new ArrayList<>();

        try (FileReader fileReader = new FileReader(filePath);
             BufferedReader bufferedReader = new BufferedReader(fileReader)
        ){
            //第一行默认为显示标签
            String first = bufferedReader.readLine();
            if(first != null) {
                String []titles = first.trim().split("\t");
                for (String str:titles){
                    labels.add(str);
                }
            }else {
                System.out.println("第一行为空");
            }
            if(labels.size() != CHANNEL){
                System.out.println("数据错误");
            }

            String line = bufferedReader.readLine();
            final int count= 2500;
            int index=0;
            while (line !=null && index<=count){
                //System.out.println(line);
                String []strs = line.trim().split("\t");
                for (int i = 0; i< CHANNEL;i++){
                    //需要处理错误数据
                    if(datas.size()<=i){
                        ArrayList<Double> items = new ArrayList<>();
                        datas.add(items);
                    }
                    double each = Double.parseDouble(strs[i]);
                    datas.get(i).add(each);
                }
                line = bufferedReader.readLine();
                index++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datas;
    }
}
